Transformer Hardware Friendly Accelerator_4
trans-fat
An FPGA Accelerator for Transformer Inference
We accelerated a BERT layer across two FPGAs, partitioned into four pipeline stages. We conduct three levels of optimization using Vitis HLS and report runtimes. The accelerator implements a transformer layer of standard BERT size, with a sequence length of 128 (which can be modified).
Instructions
This repository is designed to run on a host node with at least two Xilinx u200s. The instructions provided are specific to the the Pitt CRC fpga-n0 node, however, they may be adapted as neded for other nodes.
Dependancies
The required dependancies can be loaded using the following commands.
1 | module load xilinx/vitis/2020.2 |
🔴🟢 module:未找到命令
安装包管理工具module,🔵记得联网!!!
1 sudo apt-get install environment-modules对 module 工具进行一次初始化
1
2
3 cd /usr/share/modules/init
ls
source /usr/share/modules/init/bash🔴🟢 初始化报错
- bash:/user/bin/tclsh:没有那个文件或者目录
- bash:export:_moduleraw:不是函数
- bash:export:module:不是函数
🔵下载tcl,此错误不再出现。
1 sudo apt install tcl避免每次打开终端的时候都需要source /usr/share/modules/init/bash,需要把该命令添加到/etc/profile文件当中
- 打开/etc/profile文件:
1
2 sudo apt-get install vim
sudo vim /etc/profile- 在文件末尾加入以下语句:
1
2
3 if [ -f /usr/share/modules/init/bash ]; then
source /usr/share/modules/init/bash
fi- 在编辑完 /etc/profile 文件后,你可以按下键盘上的 “Esc” 键退出编辑模式,然后输入 :wq(意味着”write”和”quit”)保存并退出文件。
配置Module
1 echo $MODULEPATH添加环境变量
如果在安装 Environment Modules 后,输入 module list 命令时显示 “modules: command not found”,这可能意味着环境模块的可执行文件路径未正确添加到系统的 PATH 环境变量中。
你可以尝试按照以下步骤来解决该问题:
确保 Environment Modules 已成功安装。可以通过运行以下命令来检查其是否已安装:
1 dpkg -l environment-modules如果安装成功,应该会显示包的信息。
打开终端,并编辑你的用户配置文件(例如
~/.bashrc或~/.bash_profile),可以使用以下命令打开.bashrc文件:
1 nano ~/.bashrc🔵在文件的末尾添加以下行:
1 source /etc/profile.d/modules.sh🌺 nano保存文件并退出编辑器:
如果你正在使用 nano 编辑器来编辑文件,可以按照以下步骤保存文件并退出:
- 在编辑器中,你可以使用键盘上的方向键或鼠标来移动光标到你想要修改的位置,对于修改或添加文本,请直接在光标所在位置进行编辑。
- 如果要保存文件,请按下
Ctrl + O(即按住 Ctrl 键,然后按字母 “O”)。- 在底部的提示符处,你将看到 “File Name to Write” 的提示。确认文件名和路径正确,然后按下 Enter 键。
- 文件将被保存,并在底部的提示符处显示 “Wrote” 和文件名。
- 要退出 nano 编辑器,请按下
Ctrl + X。如果有未保存的更改,编辑器将询问是否要保存更改。在提示中,选择 “Y”(是)来保存更改并退出,或选择 “N”(否)来放弃更改并退出。
运行以下命令以使更改生效:
1 source ~/.bashrc
- 其他补充
🌺 vim保存文件并退出编辑器:
在 Vim 编辑器中,你可以按照以下步骤退出:
- 按下 Esc 键,以确保处于命令模式(Command mode)。
- 输入 :wq’,然后按下 Enter 键。这将保存文件并退出编辑器。如果你只想退出而不保存文件更改,可以使用 :q! 命令。
Vim 将显示一个消息,确认文件已保存(如果应用的话),然后退出到终端。
🌺 vim保存文件并退出编辑器:
在 Emacs 编辑器中,你可以按照以下步骤退出:
- 按下 Ctrl + x 键,然后按下 Ctrl + c 键。这将触发 Emacs 的退出命令。
- Emacs 将显示一个消息,询问你是否要保存文件更改。按下 y 键来保存更改,或按下 n 键来放弃更改。
- 如果选择保存更改,Emacs 将要求你指定保存的文件名或确认现有文件名。
完成保存或放弃更改后,Emacs 将退出并返回到终端。
libfaketime附带一个名为“faketime”的命令行包装,易于使用,但不公开libfaketime的所有功能。
- 安装使用
1
2 sudo apt-get update -y
sudo apt-get install -y faketime- 使用faketime命令
- 看一下此时的时间
1
2 date
2019年 11月 26日 星期二 14:46:25 CST- 直接指定时间:
1
2 faketime '2018-03-27 21:04:52' date
2018年 03月 27日 星期二 21:04:52 CST- 指定时间从10天前开始:
1
2 faketime -f '-10d' date
2019年 11月 16日 星期六 14:48:43 CST
Ubuntu上安装libfaketime
要在Ubuntu上安装libfaketime,可以按照以下步骤进行操作:
- 打开终端。您可以使用快捷键 Ctrl+Alt+T 在Ubuntu上打开终端。
- 更新软件包列表。运行以下命令以获取最新的软件包列表:
1 sudo apt update- 安装libfaketime软件包。运行以下命令来安装libfaketime:
1 sudo apt install libfaketime- 查找库文件路径:libfaketime的库文件通常位于系统的标准库目录中,例如
/usr/lib或/usr/lib64。您可以运行以下命令来查找libfaketime库文件的路径:这将列出libfaketime软件包安装的文件列表,您可以在其中找到libfaketime的库文件路径。
1 dpkg -L libfaketime- 验证安装。安装完成后,可以运行以下命令来验证libfaketime> 是否成功安装:
如果成功安装,将显示libfaketime的版本信息。
1 faketime --version
要确保正确安装libfaketime并指定正确的库路径,可以按照以下步骤进行操作:- 设置LD_PRELOAD环境变量:打开终端,并使用
export命令设置LD_PRELOAD环境变量,将libfaketime库的路径指定为其值:
1 export LD_PRELOAD=/path/to/libfaketime.so
- 如果文件名以’.so.1’结尾而不是’.so’,这表示安装的是libfaketime的共享库的符号链接(symbolic link),而不是直接的库文件。
- 在Linux系统中,共享库通常具有主版本号、次版本号和修订号。例如,libfaketime.so.1表示libfaketime的共享库的主版本号为1。这样的命名约定允许在保持主版本不变的情况下,进行修订和更新。
将/path/to/libfaketime.so替换为您在步骤2中找到的libfaketime库文件的实际路径。
1 export LD_PRELOAD=/path/to/libfaketime.so.1- 验证设置:运行以下命令来验证是否已正确设置LD_PRELOAD环境变量:
这将显示LD_PRELOAD环境变量的值,确保它指向libfaketime库的路径。
1 echo $LD_PRELOAD- 加载
libfaketime是一个用户空间工具,不需要加载到内核中。它通过在程序执行时进行动态链接来实现对程序执行时间的修改。
Building
All building is performed in the fpga/ directory. Navigate there and enter the following command.
1 | faketime 'last year' make all TARGET=<hw, hw_emu, sw_emu> VERSION=<0, 1, 2, 3> PART=<fpga1, fpga2, all> JOBS=< |
If building for hardware the output artifacts will automatically be coppied into /builds/v#/fpga#/.
Running
To run all enter make test VERSION=<0, 1, 2, 3> PART=all in the fpga/ directory.
Individual fpga builds can be run directly using the host and executable in the desired builds/ directory.
Optimization Versions
v0
- None
v1
- Linear layer tiling
- Buffering of input and output data
- Unrolling of multiplication inner loops
v2
- Transpose A matmul input
- Cache line of A.T
- Increase tile size in j dimension
- Unrolling of computation in attention heads
v3
- Stream DDR inputs/outputs in linear layers
Results
| Version | Latency (ms) | ||
|---|---|---|---|
| fpga1 | fpga2 | all | |
| v0 | 4723.71 | 10950.90 | 15676.30 |
| v1 | 274.98 | 120.91 | 397.45 |
| v2 | 48.36 | 95.60 | 145.27 |
| v3 | 35.03 | 71.76 | 110.99 |